package com.xh.demo.mq.amqp.dlx;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.xh.demo.mq.stream.source.DelayMessageOutputSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author wen
 * @apiNote DelayMsgProducer
 * @since 2021-05-31
 */
@Slf4j
@Component
public class DelayMsgProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送延时消息
     */
    public void sendDelayedMsg(String message, long delayTime) {
        rabbitTemplate.convertAndSend(
                "delay_exchange",
                "delay_key",
                message,
                msg -> {
                    //配置消息的过期时间
                    msg.getMessageProperties().setDelay((int) delayTime);
                    return msg;
                }
        );
        log.error("消息通知-AMQP-DLX-发送MQ异步消息-sendMsg【{}】【{}】【{}】", DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN), delayTime, message);
    }

}
